package inset;

import java.awt.geom.Point2D;
import java.util.ArrayList;

public class VertexManager extends ArrayList<VertexManager.Vertex>
{
    /*
     * Hopefully return the same reference to identical points 
     */
    public Point2D getVertexForPoint(Point2D point)
    {
	int index = indexOf(new Vertex(point));
	
	if (index >= 0)
	    return get(index).pt;
	else
	{
	    add(new Vertex(point));
	    return point;
	}	
    }
    
    class Vertex
    {
	public Vertex(Point2D pt)
	{
	    this.pt = pt;
	}
	
	@Override
	public boolean equals(Object obj)
	{
	    if (obj != null && obj instanceof Vertex)
	
	   {
		return pt.distance(((Vertex) obj).pt) < TOL;
	   }
	    
	    return false;
	}

	private double TOL = 5e-7;

	Point2D pt;
    }
}